using DotNet.Utilities.ConsoleHelper;
using PropertyChanged;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using DotNet.Utilities;
using WPFPractice.Models;
using WPFPractice.Utils;
using WPFPractice.ViewModels;
using WPFTemplateLib.WpfHelpers;

namespace WPFPractice.Windows
{
	public partial class WinRadioButton : Window
	{
		private RadioButtonViewModel _vm;

		public WinRadioButton()
		{
			InitializeComponent();
			DataContext = _vm = new RadioButtonViewModel();
		}

		private bool _lastChecked;
		private void RbCanUncheck_OnClick(object sender, RoutedEventArgs e)
		{
			if(_lastChecked)
			{
				RbCanUncheck.IsChecked = false;
				_lastChecked = false;
			}
			else
			{
				RbCanUncheck.IsChecked = true;
				_lastChecked = true;
			}
		}
	}
}

namespace WPFPractice.ViewModels
{
	[AddINotifyPropertyChangedInterface]
	public class RadioButtonViewModel : MyViewModelBase
	{
		#region 成员

		#endregion

		#region 绑定

		public ObservableCollection<Enum> Options { get; set; }

		public Enum SelectedItem { get; set; }

		public EditTypeEnum? SelectedEditType { get; set; }

		public int SelectedNumber { get; set; }

		#endregion

		#region 命令

		/// <summary>
		/// 切换到新增模式命令
		/// </summary>
		public ICommand AddNewCmd { get; set; }

		#region [命令] 清空 RadioButton 选中项
		private RelayCommand _ClearRadioSelectCmd;
		public RelayCommand ClearRadioSelectCmd => _ClearRadioSelectCmd ??= new RelayCommand(ExecuteClearRadioSelectCmd);
		private void ExecuteClearRadioSelectCmd()
		{
			SelectedEditType = null;
		}
		#endregion

		#endregion

		public RadioButtonViewModel()
		{
			Console.SetOut(new ConsoleWriter(s =>
			{
				ShowInfoUc(s);
			}));

			SetCommandMethod();

			_ = InitAsync();
		}

		/// <summary>
		/// 异步初始化
		/// </summary>
		private async Task InitAsync()
		{
			await Task.Delay(1);

			Console.WriteLine("演示 RadioButton 取消选中");
			Console.WriteLine("演示 RadioButton 数据绑定情况下的获取选择项");
			Console.WriteLine("演示 带边框的 RadioButton 样式");

			Options = new ObservableCollection<Enum>(EnumExtension.GetEnumList<EditTypeEnum>().Cast<Enum>());
		}

		/// <summary>
		/// 命令方法赋值(在构造函数中调用)
		/// </summary>
		private void SetCommandMethod()
		{
			AddNewCmd ??= new RelayCommand(o => true, o =>
			{

			});
		}
	}
}
